﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MainWindow.xaml.cs" company="Jens Horstmann">
//   Copyright (c) Jens Horstmann. All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace MiniMusicPlayer
{
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Forms;
    using System.Windows.Input;

    using Microsoft.Win32;

    using MiniMusicPlayer.Classes;
    using MiniMusicPlayer.Windows;

    using Button = System.Windows.Controls.Button;
    using ContextMenu = System.Windows.Controls.ContextMenu;
    using DragEventArgs = System.Windows.DragEventArgs;
    using MessageBox = System.Windows.MessageBox;

    /// <summary>
    /// Interaction logic for MainWindow.xaml.
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        /// <summary>
        /// The context menu notify icon.
        /// </summary>
        private ContextMenu contextMenuNotifyIcon;

        /// <summary>
        /// The drop handler.
        /// </summary>
        private DropHandler dropHandler;

        /// <summary>
        /// The grid buttons.
        /// </summary>
        private List<Button> gridButtons;

        /// <summary>
        /// The notify icon.
        /// </summary>
        private NotifyIcon notifyIcon;

        /// <summary>
        /// The window about.
        /// </summary>
        private WindowAbout windowAbout;

        /// <summary>
        /// The window playlist.
        /// </summary>
        private WindowPlaylist windowPlaylist;

        /// <summary>
        /// The window settings.
        /// </summary>
        private WindowSettings windowSettings;

        private MusicPlayer musicPlayer;

        /// <summary>
        /// Initializes a new instance of the <see cref="MainWindow"/> class.
        /// </summary>
        public MainWindow()
        {
            this.InitializeComponent();

            this.Controls = new ObservableCollection<UIElement>();
            
        }

        /// <summary>
        /// The music player.
        /// </summary>
        public MusicPlayer MusicPlayer
        {
            get
            {
                return this.musicPlayer;
            }
            set
            {
                this.musicPlayer = value;
                this.OnPropertyChanged("MusicPlayer");
            }
        }

        /// <summary>
        /// Gets or sets the controls.
        /// </summary>
        public ObservableCollection<UIElement> Controls { get; set; }

        /// <summary>
        /// The move.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void Move(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        /// <summary>
        /// The play pause.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void PlayPause(object sender, RoutedEventArgs e)
        {
            this.MusicPlayer.PlayPause();
            if (this.MusicPlayer.IsPlaying)
            {
                this.ImagePlayPause.Style = this.FindResource("ButtonPauseStyle") as Style;
            }
            else
            {
                this.ImagePlayPause.Style = this.FindResource("ButtonPlayStyle") as Style;
            }
        }

        /// <summary>
        /// The settings clicked.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void MenuClicked(object sender, RoutedEventArgs e)
        {
            if (this.CirclePanel.Visibility == Visibility.Visible)
            {
                this.CirclePanel.Visibility = Visibility.Hidden;
                this.StackPanelOptions.Visibility = Visibility.Hidden;
            }
            else
            {
                this.CirclePanel.Visibility = Visibility.Visible;
                this.StackPanelOptions.Visibility = Visibility.Visible;
            }
        }

        /// <summary>
        /// The window deactivated.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void WindowDeactivated(object sender, EventArgs e)
        {
            this.CirclePanel.Visibility = Visibility.Hidden;
            this.StackPanelOptions.Visibility = Visibility.Hidden;
            this.ImagePlaylist.Style = this.ImagePlaylist.Style = this.FindResource("ButtonPlaylistActivate") as Style;
        }

        /// <summary>
        /// The hide windows.
        /// </summary>
        public void HideWindows()
        {
            this.windowPlaylist.Hide();
            this.windowAbout.Hide();
            this.windowSettings.Hide();
        }

        /// <summary>
        /// The items dropped.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void ItemsDropped(object sender, DragEventArgs e)
        {
            this.dropHandler.HandleDrop(e);
        }

        /// <summary>
        /// The save buttons.
        /// </summary>
        private void SaveButtons()
        {
            if (this.gridButtons == null)
            {
                this.gridButtons = new List<Button>();
                foreach (var child in this.CirclePanel.Children)
                {
                    this.gridButtons.Add((Button)child);
                }

                foreach (var child in this.StackPanelOptions.Children)
                {
                    this.gridButtons.Add((Button)child);
                }
            }
        }

        /// <summary>
        /// The activate stack panel layout.
        /// </summary>
        public void ActivateStackPanelLayout()
        {
            this.SaveButtons();
            this.CirclePanel.Children.Clear();
            this.StackPanelOptions.Children.Clear();

            foreach (var gridButton in this.gridButtons)
            {
                this.StackPanelOptions.Children.Add(gridButton);
            }
        }

        /// <summary>
        /// The activate circle panel layout.
        /// </summary>
        public void ActivateCirclePanelLayout()
        {
            this.SaveButtons();
            this.CirclePanel.Children.Clear();
            this.StackPanelOptions.Children.Clear();

            foreach (var gridButton in this.gridButtons)
            {
                this.CirclePanel.Children.Add(gridButton);
            }
        }

        /// <summary>
        /// The window loaded.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void WindowLoaded(object sender, RoutedEventArgs e)
        {
            this.MusicPlayer = new MusicPlayer();
            this.dropHandler = new DropHandler(this.MusicPlayer.Playlist);

            this.contextMenuNotifyIcon = this.FindResource("ContextMenuNotifyIcon") as ContextMenu;

            this.notifyIcon = new NotifyIcon();
            this.notifyIcon.Icon = Properties.Resources.Music;
            this.notifyIcon.Visible = true;
            this.notifyIcon.Click += this.NotifyIconClicked;

            this.windowPlaylist = new WindowPlaylist(this);
            this.windowAbout = new WindowAbout();
            this.windowSettings = new WindowSettings(this);

            // this.PrintMediaPlayerVersion();
        }

        /// <summary>
        /// The notify icon clicked.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void NotifyIconClicked(object sender, EventArgs e)
        {
            this.contextMenuNotifyIcon.IsOpen = true;
        }

        /// <summary>
        /// The print media player version.
        /// </summary>
        private void PrintMediaPlayerVersion()
        {
            var myKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\MediaPlayer\\PlayerUpgrade", false);
            string version = null;
            if (myKey != null)
            {
                version = (string)myKey.GetValue("PlayerVersion");
            }

            if (version != null)
            {
                MessageBox.Show(version);
            }
            else
            {
                MessageBox.Show("No MediaPlayer installed. Implement installation of WMP.");
            }
        }

        /// <summary>
        /// The next.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void Next(object sender, RoutedEventArgs e)
        {
            this.MusicPlayer.NextSong();
        }

        /// <summary>
        /// The play mode clicked.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void PlayModeClicked(object sender, RoutedEventArgs e)
        {
            switch (this.MusicPlayer.Playlist.PlaylistMode)
            {
                case PlaylistMode.Random:
                    {
                        this.ImagePlayMode.Style = this.FindResource("ButtonPlayModeLoop") as Style;
                        this.MusicPlayer.Playlist.PlaylistMode = PlaylistMode.Loop;
                        break;
                    }

                case PlaylistMode.Loop:
                    {
                        this.ImagePlayMode.Style = this.FindResource("ButtonPlayModeSingle") as Style;
                        this.MusicPlayer.Playlist.PlaylistMode = PlaylistMode.Single;
                        break;
                    }

                case PlaylistMode.Single:
                    {
                        this.ImagePlayMode.Style = this.FindResource("ButtonPlayModeRandom") as Style;
                        this.MusicPlayer.Playlist.PlaylistMode = PlaylistMode.Random;
                        break;
                    }
            }
        }

        /// <summary>
        /// The mute clicked.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void MuteClicked(object sender, RoutedEventArgs e)
        {
            this.MusicPlayer.Mute();

            if (this.MusicPlayer.MediaPlayer.IsMuted)
            {
                this.ImageMute.Style = this.FindResource("ButtonUnMute") as Style;
            }
            else
            {
                this.ImageMute.Style = this.FindResource("ButtonMute") as Style;
            }
        }

        /// <summary>
        /// The volume up.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void VolumeUp(object sender, RoutedEventArgs e)
        {
            this.MusicPlayer.VolumeUp();
        }

        /// <summary>
        /// The volume down.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void VolumeDown(object sender, RoutedEventArgs e)
        {
            this.MusicPlayer.VolumeDown();
        }

        /// <summary>
        /// The close window.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void CloseWindow(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// The playlist clicked.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void PlaylistClicked(object sender, RoutedEventArgs e)
        {
            this.windowPlaylist.Show();
        }

        /// <summary>
        /// The about clicked.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void AboutClicked(object sender, RoutedEventArgs e)
        {
            this.windowAbout.Show();
        }

        /// <summary>
        /// The window closing.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void WindowClosing(object sender, CancelEventArgs e)
        {
            this.windowAbout.Close();
            this.windowPlaylist.Close();
            this.windowSettings.Close();
        }

        /// <summary>
        /// The previous song clicked.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void PreviousSongClicked(object sender, RoutedEventArgs e)
        {
            this.MusicPlayer.PreviousSong();
        }

        /// <summary>
        /// The settings clicked.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void SettingsClicked(object sender, RoutedEventArgs e)
        {
            this.windowSettings.Show();
        }

        protected void OnPropertyChanged(string name)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}